<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author FIRE
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
   
    @register="registerDrawer"
    @ok="handleSubmit"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsProjEnexPmApproachForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { PmApproach, pmApproachSave, pmApproachForm } from '/@/api/proj/enex/pmApproach';

  //  :okAuth="'proj:enex:pmApproach:edit'"
  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('proj.enex.pmApproach');
  const { showMessage } = useMessage();
  const record = ref<PmApproach>({} as PmApproach);
  const getTitle = computed(() => ({
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增进场撤场') : t('编辑进场撤场'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('创建时间'),
      field: 'createDate',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
      },
    },
    {
      label: t('工程名称'),
      field: 'name',
      component: 'Input',
      componentProps: {
        maxlength: 500,
      },
    },
    {
      label: t('工程编码'),
      field: 'pcode',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('延期时间'),
      field: 'delayedu',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('发送进场通知时间'),
      field: 'launchpredate',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
      },
    },
    {
      label: t('要求进场日期'),
      field: 'predate',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD',
        showTime: false,
      },
    },
    {
      label: t('发送撤场通知时间'),
      field: 'launchprewdate',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
      },
    },
    {
      label: t('要求撤场日期'),
      field: 'prewithdrawdate',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD',
        showTime: false,
      },
    },
    {
      label: t('状态'),
      field: 'state',
      component: 'Select',
      componentProps: {
        dictType: 'zf_proj_jcsg_status',
        allowClear: true,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('通知作废时间'),
      field: 'scrapdate',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
      },
    },
    {
      label: t('确认进场日期'),
      field: 'approachdate',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD',
        showTime: false,
      },
    },
    {
      label: t('现场环境'),
      field: 'approachedu',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('确认撤场日期'),
      field: 'withdrawdate',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD',
        showTime: false,
      },
    },
    {
      label: t('撤场现场环境'),
      field: 'withdrawedu',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('审核人编码'),
      field: 'checkno',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('审核人'),
      field: 'checker',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('审核时间'),
      field: 'checkdate',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
      },
    },
    {
      label: t('进场备注'),
      field: 'approachremarks',
      component: 'Input',
      componentProps: {
        maxlength: 255,
      },
    },
    {
      label: t('图片上传'),
      field: 'dataMap',
      component: 'Upload',
      componentProps: {
        loadTime: computed(() => record.value.__t),
        bizKey: computed(() => record.value.id),
        bizType: 'pmApproach_image',
        uploadType: 'image',
      },
      colProps: { lg: 24, md: 24 },
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 24, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await pmApproachForm(data);
    record.value = (res.pmApproach || {}) as PmApproach;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        id: record.value.id,
      };
      // console.log('submit', params, data, record);
      const res = await pmApproachSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
